package ui.fragment.PosControl;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import net.ck.hzq.R;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import base.BaseBiz;
import base.LazyFragment;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.OnLoadMoreListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import cn.finalteam.loadingviewfinal.RecyclerViewFinal;
import coom.MyType;
import event.CloseActEvent;
import event.HasPushEvent;
import event.NetErrorEvent;
import ui.activity.poscontrol.adapter.RedBallAdapter;
import ui.activity.poscontrol.beanmodel.RedBallBean;
import ui.activity.poscontrol.biz.MyPosControlBiz;

/**
 * Created by fangjunsong on 2019年3月19日17:03:30.
 * <p>
 * 回拨记录-接收的
 */

public class RedBallHasUseFra extends LazyFragment{


    MyPosControlBiz biz;


    @BindView(R.id.pushpeople_list)
    RecyclerViewFinal rv;

    @BindView(R.id.pull_refresh_ptr)
    PtrClassicFrameLayout ptr;

    @BindView(R.id.nomessage)
    LinearLayout nomessage;


    List<RedBallBean.RecordsBean> databeans = new ArrayList<>();
    RedBallAdapter adp;
    private int page = 1;
    private boolean isPrepared;// 界面否
    private boolean isALeady = true;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.activity_changebackrecord, container, false);
        ButterKnife.bind(this, view);
        biz = new MyPosControlBiz();
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
//        rv.setItemAnimator(new DefaultItemAnimator());
//        rv.addItemDecoration(new DividerItemDecoration(getActivity(), 1));

        isPrepared = true;


        ptr.setLastUpdateTimeRelateObject(this);
        ptr.disableWhenHorizontalMove(true);
        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getData();
            }
        });
        rv.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                loadMoredata();
            }
        });
        return view;
    }


    @Override
    protected void lazyLoad() {
        if (isPrepared && isVisible && isALeady) {
            // TODO 在这里处理逻辑
            getData();
            isALeady = false;
        }
    }
    private void loadMoredata(){
        databeans.clear();
        biz.getRedBallList(2,page,100,new BaseBiz.Callback<RedBallBean>() {
            @Override
            public void onSuccess(RedBallBean pushBackPeopleBean) {

                page+=1;
                rv.onLoadMoreComplete();
                if(pushBackPeopleBean.getRecords().size()<100){
                    rv.setHasLoadMore(false);
                }
                databeans.addAll(pushBackPeopleBean.getRecords());
                if(null == adp){
                    adp = new RedBallAdapter(getActivity(),databeans,2);
                    rv.setAdapter(adp);
                }else{
                    adp.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(RedBallBean pushBackPeopleBean) {
                rv.onLoadMoreComplete();
            }
        });
    }
    private void getData(){
        databeans.clear();
        biz.getRedBallList(2,1,100,new BaseBiz.Callback<RedBallBean>() {
            @Override
            public void onSuccess(RedBallBean pushBackPeopleBean) {

                ptr.onRefreshComplete();
                page+=1;
                databeans.addAll(pushBackPeopleBean.getRecords());
                if (databeans.size()==0){
                    nomessage.setVisibility(View.VISIBLE);
                }
                if(null == adp){
                    adp = new RedBallAdapter(getActivity(),databeans,2);
                    rv.setAdapter(adp);
                }else{
                    adp.notifyDataSetChanged();
                }
                if(pushBackPeopleBean.getRecords().size()<100){
                    rv.setHasLoadMore(false);
                }else{
                    rv.setHasLoadMore(true);
                }
            }

            @Override
            public void onFailure(RedBallBean pushBackPeopleBean) {
                ptr.onRefreshComplete();
            }
        });
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        biz.onDestory();
        ButterKnife.bind(getActivity()).unbind();
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCloseActEvent(CloseActEvent event) {
        if (event.getType() == MyType.CompleteOrder) {
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refrshdata(HasPushEvent event) {
       getData();
    }
}